#include <QApplication>
#include "ElaApplication.h"
#include "ElaTheme.h"
#include "ElaWidget.h"
#include "ElaToolButton.h"
#include "ElaMenu.h"
#include "ElaIcon.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QCheckBox>
#include <QDebug>

class ElaToolButtonDemo : public ElaWidget
{
    Q_OBJECT

  public:
    ElaToolButtonDemo(QWidget *parent = nullptr) : ElaWidget(parent)
    {
        setWindowTitle("ElaToolButton 功能演示");
        resize(700, 600);
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

        setupUI();
        setupConnections();
    }

  private:
    void setupUI()
    {
        QVBoxLayout *mainLayout = new QVBoxLayout(this);

        // 1. 基础工具按钮展示
        mainLayout->addWidget(createBasicButtonGroup());

        // 2. 圆角按钮展示
        mainLayout->addWidget(createBorderRadiusGroup());

        // 3. 选中状态按钮展示
        mainLayout->addWidget(createSelectedStateGroup());

        // 4. 透明按钮展示
        mainLayout->addWidget(createTransparentButtonGroup());

        // 5. 带菜单的按钮展示
        mainLayout->addWidget(createMenuButtonGroup());

        // 6. 图标按钮展示
        mainLayout->addWidget(createIconButtonGroup());

        mainLayout->addStretch();
    }

    // 创建基础工具按钮组
    QGroupBox *createBasicButtonGroup()
    {
        QGroupBox *group = new QGroupBox("基础工具按钮", this);
        QHBoxLayout *layout = new QHBoxLayout(group);

        // 基础工具按钮1
        ElaToolButton *btn1 = new ElaToolButton(this);
        btn1->setText("基础按钮");
        btn1->setToolTip("这是一个基础工具按钮");
        btn1->setToolButtonStyle(Qt::ToolButtonTextOnly);  // 只显示文本

        // 基础工具按钮2 - 带图标
        ElaToolButton *btn2 = new ElaToolButton(this);
        btn2->setText("带图标文本在图标旁");
        btn2->setElaIcon(ElaIconType::House);
        btn2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);  // 文本在图标旁边

        // 基础工具按钮3 - 带图标 图标在- 图标在文本下方
        ElaToolButton *btn3 = new ElaToolButton(this);
        btn3->setText("带图标文本在图标下");
        btn3->setElaIcon(ElaIconType::User);
        btn3->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);  // 文本在图标下方

        layout->addWidget(btn1);
        layout->addWidget(btn2);
        layout->addWidget(btn3);

        m_basicButtons << btn1 << btn2 << btn3;

        return group;
    }

    // 创建圆角按钮组 - 展示不同圆角效果
    QGroupBox *createBorderRadiusGroup()
    {
        QGroupBox *group = new QGroupBox("圆角工具按钮", this);
        QHBoxLayout *layout = new QHBoxLayout(group);

        // 直角按钮
        ElaToolButton *btn0 = new ElaToolButton(this);
        btn0->setText("直角(0)");
        btn0->setBorderRadius(0);
        btn0->setElaIcon(ElaIconType::GearComplex);
        btn0->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        // 小圆角按钮
        ElaToolButton *btn2 = new ElaToolButton(this);
        btn2->setText("小圆角(2)");
        btn2->setBorderRadius(2);
        btn2->setElaIcon(ElaIconType::MagnifyingGlass);
        btn2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        // 默认圆角按钮
        ElaToolButton *btn5 = new ElaToolButton(this);
        btn5->setText("默认(5)");
        btn5->setBorderRadius(5);
        btn5->setElaIcon(ElaIconType::User);
        btn5->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        // 大圆角按钮
        ElaToolButton *btn10 = new ElaToolButton(this);
        btn10->setText("大圆角(10)");
        btn10->setBorderRadius(10);
        btn10->setElaIcon(ElaIconType::Heart);
        btn10->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        // 圆形按钮
        ElaToolButton *btn20 = new ElaToolButton(this);
        btn20->setText("圆形(20)");
        btn20->setBorderRadius(20);
        btn20->setElaIcon(ElaIconType::Star);
        btn20->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        layout->addWidget(btn0);
        layout->addWidget(btn2);
        layout->addWidget(btn5);
        layout->addWidget(btn10);
        layout->addWidget(btn20);

        m_radiusButtons << btn0 << btn2 << btn5 << btn10 << btn20;

        return group;
    }

    // 创建选中状态按钮组
    QGroupBox *createSelectedStateGroup()
    {
        QGroupBox *group = new QGroupBox("选中状态工具按钮", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("选中状态的按钮会保持高亮显示，常用于导航菜单", this);

        QHBoxLayout *buttonLayout = new QHBoxLayout();

        // 未选中按钮
        ElaToolButton *unselectedBtn = new ElaToolButton(this);
        unselectedBtn->setText("未选中");
        unselectedBtn->setElaIcon(ElaIconType::File);
        unselectedBtn->setIsSelected(false);
        unselectedBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        // 选中按钮
        ElaToolButton *selectedBtn = new ElaToolButton(this);
        selectedBtn->setText("已选中");
        selectedBtn->setElaIcon(ElaIconType::File);
        selectedBtn->setIsSelected(true);
        selectedBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        // 切换按钮
        ElaToolButton *toggleBtn = new ElaToolButton(this);
        toggleBtn->setText("点击切换");
        toggleBtn->setElaIcon(ElaIconType::Check);
        toggleBtn->setCheckable(true);
        toggleBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        buttonLayout->addWidget(unselectedBtn);
        buttonLayout->addWidget(selectedBtn);
        buttonLayout->addWidget(toggleBtn);

        layout->addWidget(infoLabel);
        layout->addLayout(buttonLayout);

        m_selectedButtons << unselectedBtn << selectedBtn << toggleBtn;
        m_toggleButton = toggleBtn;

        return group;
    }

    // 创建透明按钮组
    QGroupBox *createTransparentButtonGroup()
    {
        QGroupBox *group = new QGroupBox("透明工具按钮", this);
        QHBoxLayout *layout = new QHBoxLayout(group);

        // 普通按钮
        ElaToolButton *normalBtn = new ElaToolButton(this);
        normalBtn->setText("普通按钮");
        normalBtn->setElaIcon(ElaIconType::Camera);
        normalBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        // 透明按钮
        ElaToolButton *transparentBtn = new ElaToolButton(this);
        transparentBtn->setText("透明按钮");
        transparentBtn->setElaIcon(ElaIconType::Camera);
        transparentBtn->setIsTransparent(true);
        transparentBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        // 透明选中按钮
        ElaToolButton *transparentSelectedBtn = new ElaToolButton(this);
        transparentSelectedBtn->setText("透明选中");
        transparentSelectedBtn->setElaIcon(ElaIconType::Camera);
        transparentSelectedBtn->setIsTransparent(true);
        transparentSelectedBtn->setIsSelected(true);
        transparentSelectedBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        layout->addWidget(normalBtn);
        layout->addWidget(transparentBtn);
        layout->addWidget(transparentSelectedBtn);

        m_transparentButtons << normalBtn << transparentBtn << transparentSelectedBtn;

        return group;
    }

    // 创建带菜单的按钮组
    QGroupBox *createMenuButtonGroup()
    {
        QGroupBox *group = new QGroupBox("带菜单的工具按钮", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("点击按钮查看菜单展开动画效果", this);

        QHBoxLayout *buttonLayout = new QHBoxLayout();

        // 文件菜单按钮
        ElaToolButton *fileMenuBtn = new ElaToolButton(this);
        fileMenuBtn->setText("文件菜单");
        fileMenuBtn->setElaIcon(ElaIconType::Folder);
        fileMenuBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        // 创建文件菜单
        ElaMenu *fileMenu = new ElaMenu(this);
        fileMenu->addAction("新建文件");
        fileMenu->addAction("打开文件");
        fileMenu->addAction("保存文件");
        fileMenu->addSeparator();
        fileMenu->addAction("退出");
        fileMenuBtn->setMenu(fileMenu);

        // 编辑菜单按钮
        ElaToolButton *editMenuBtn = new ElaToolButton(this);
        editMenuBtn->setText("编辑菜单");
        editMenuBtn->setElaIcon(ElaIconType::CreditCard);
        editMenuBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        // 创建编辑菜单
        ElaMenu *editMenu = new ElaMenu(this);
        editMenu->addAction("撤销");
        editMenu->addAction("重做");
        editMenu->addSeparator();
        editMenu->addAction("复制");
        editMenu->addAction("粘贴");
        editMenu->addAction("剪切");
        editMenuBtn->setMenu(editMenu);

        // 视图菜单按钮
        ElaToolButton *viewMenuBtn = new ElaToolButton(this);
        viewMenuBtn->setText("视图菜单");
        viewMenuBtn->setElaIcon(ElaIconType::Eye);
        viewMenuBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        // 创建视图菜单
        ElaMenu *viewMenu = new ElaMenu(this);
        viewMenu->addAction("放大");
        viewMenu->addAction("缩小");
        viewMenu->addAction("实际大小");
        viewMenu->addSeparator();
        viewMenu->addAction("全屏显示");
        viewMenuBtn->setMenu(viewMenu);

        buttonLayout->addWidget(fileMenuBtn);
        buttonLayout->addWidget(editMenuBtn);
        buttonLayout->addWidget(viewMenuBtn);

        layout->addWidget(infoLabel);
        layout->addLayout(buttonLayout);

        m_menuButtons << fileMenuBtn << editMenuBtn << viewMenuBtn;

        return group;
    }

    // 创建图标按钮组 - 这里保持只显示图标
    QGroupBox *createIconButtonGroup()
    {
        QGroupBox *group = new QGroupBox("图标工具按钮", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示不同的Ela图标", this);

        QHBoxLayout *iconLayout1 = new QHBoxLayout();
        QHBoxLayout *iconLayout2 = new QHBoxLayout();

        // 常用图标按钮 - 保持只显示图标
        ElaToolButton *homeBtn = new ElaToolButton(this);
        homeBtn->setElaIcon(ElaIconType::House);
        homeBtn->setToolTip("首页");
        homeBtn->setToolButtonStyle(Qt::ToolButtonIconOnly);  // 只显示图标

        ElaToolButton *searchBtn = new ElaToolButton(this);
        searchBtn->setElaIcon(ElaIconType::MagnifyingGlass);
        searchBtn->setToolTip("搜索");
        searchBtn->setToolButtonStyle(Qt::ToolButtonIconOnly);

        ElaToolButton *settingsBtn = new ElaToolButton(this);
        settingsBtn->setElaIcon(ElaIconType::GearComplex);
        settingsBtn->setToolTip("设置");
        settingsBtn->setToolButtonStyle(Qt::ToolButtonIconOnly);

        ElaToolButton *userBtn = new ElaToolButton(this);
        userBtn->setElaIcon(ElaIconType::User);
        userBtn->setToolTip("用户");
        userBtn->setToolButtonStyle(Qt::ToolButtonIconOnly);

        ElaToolButton *heartBtn = new ElaToolButton(this);
        heartBtn->setElaIcon(ElaIconType::Heart);
        heartBtn->setToolTip("收藏");
        heartBtn->setToolButtonStyle(Qt::ToolButtonIconOnly);

        ElaToolButton *starBtn = new ElaToolButton(this);
        starBtn->setElaIcon(ElaIconType::Star);
        starBtn->setToolTip("评分");
        starBtn->setToolButtonStyle(Qt::ToolButtonIconOnly);

        ElaToolButton *downloadBtn = new ElaToolButton(this);
        downloadBtn->setElaIcon(ElaIconType::Download);
        downloadBtn->setToolTip("下载");
        downloadBtn->setToolButtonStyle(Qt::ToolButtonIconOnly);

        ElaToolButton *uploadBtn = new ElaToolButton(this);
        uploadBtn->setElaIcon(ElaIconType::Upload);
        uploadBtn->setToolTip("上传");
        uploadBtn->setToolButtonStyle(Qt::ToolButtonIconOnly);

        iconLayout1->addWidget(homeBtn);
        iconLayout1->addWidget(searchBtn);
        iconLayout1->addWidget(settingsBtn);
        iconLayout1->addWidget(userBtn);

        iconLayout2->addWidget(heartBtn);
        iconLayout2->addWidget(starBtn);
        iconLayout2->addWidget(downloadBtn);
        iconLayout2->addWidget(uploadBtn);

        layout->addWidget(infoLabel);
        layout->addLayout(iconLayout1);
        layout->addLayout(iconLayout2);

        m_iconButtons << homeBtn << searchBtn << settingsBtn << userBtn << heartBtn << starBtn << downloadBtn << uploadBtn;

        return group;
    }

    void setupConnections()
    {
        // 切换按钮的选中状态
        connect(m_toggleButton, &ElaToolButton::toggled, this, [this](bool checked) {
            m_toggleButton->setIsSelected(checked);
            if (checked)
            {
                m_toggleButton->setText("已选中");
            }
            else
            {
                m_toggleButton->setText("未选中");
            }
        });

        // 图标按钮点击事件
        for (ElaToolButton *btn : m_iconButtons)
        {
            connect(btn, &ElaToolButton::clicked, this, [btn]() { qDebug() << "图标按钮被点击:" << btn->toolTip(); });
        }

        // 基础按钮点击事件
        for (int i = 0; i < m_basicButtons.size(); ++i)
        {
            connect(m_basicButtons[i], &ElaToolButton::clicked, this, [i]() { qDebug() << "基础按钮" << i + 1 << "被点击"; });
        }
    }

  private:
    QList<ElaToolButton *> m_basicButtons;
    QList<ElaToolButton *> m_radiusButtons;
    QList<ElaToolButton *> m_selectedButtons;
    QList<ElaToolButton *> m_transparentButtons;
    QList<ElaToolButton *> m_menuButtons;
    QList<ElaToolButton *> m_iconButtons;
    ElaToolButton *m_toggleButton = nullptr;
};

int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#else
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);
    eApp->init();

    ElaToolButtonDemo demo;
    demo.moveToCenter();
    demo.show();

    return app.exec();
}

#include "ElaToolButtonTest.moc"